package com.shenyue.nbtownlet.activity;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.*;
import com.shenyue.nbtownlet.R;
import com.shenyue.nbtownlet.base.BaseActivity;
import com.shenyue.nbtownlet.databinding.ActivityTestBinding;
import com.shenyue.nbtownlet.databinding.ActivityVideoBinding;
import com.shenyue.nbtownlet.util.ActivityUtil;

/**
 * 测试类
 */

public class VideoActivity extends BaseActivity {
    private ActivityVideoBinding binding;
    private Context mContext;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = DataBindingUtil.setContentView(this, R.layout.activity_video);
        mContext = this;
        /*********视频录制 拍照***********/
        //设置视频保存路径
        binding.cameraView.setSaveVideoPath(ActivityUtil.getVideoPath(mContext));
        //设置只能录像或只能拍照或两种都可以（默认两种都可以）
        binding.cameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);
        //设置视频质量
        binding.cameraView.setMediaQuality(JCameraView.MEDIA_QUALITY_MIDDLE);
        //JCameraView监听
        binding.cameraView.setErrorLisenter(new ErrorListener() {
            @Override
            public void onError() {

            }

            @Override
            public void AudioPermissionError() {

            }
        });
        binding.cameraView.setJCameraLisenter(new JCameraListener() {
            @Override
            public void captureSuccess(Bitmap bitmap) {

            }

            @Override
            public void recordSuccess(final String url, Bitmap firstFrame) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        callback(url);
                    }
                }, 32);
            }
        });
        /*********视频录制 拍照***********/
    }

    private void callback(String url) {
        Intent intent = new Intent();
        intent.putExtra("url", url);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        binding.cameraView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        binding.cameraView.onPause();
    }
}
